

package com.hulk.dryad.web.oauth2.base.mobile;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hulk.dryad.manage.security.component.DryadAuthExceptionEntryPoint;
import com.hulk.dryad.web.oauth2.base.handler.DryadLoginSuccessHandler;
import com.hulk.dryad.web.oauth2.base.service.DryadUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationEventPublisher;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author hulk
 * @date 2019/8/5 手机号登录配置入口
 */
public class MobileSecurityConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    @Autowired
    private  ObjectMapper objectMapper;
    @Autowired
    private  AuthenticationEventPublisher defaultAuthenticationEventPublisher;
    @Autowired
    private  DryadUserDetailsService dryadUserDetailsServiceImpl;
    @Autowired
	private DryadLoginSuccessHandler dryadLoginSuccessHandler;

    @Override
    public void configure(HttpSecurity http) {
        MobileAuthenticationFilter mobileAuthenticationFilter = new MobileAuthenticationFilter();
        mobileAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        mobileAuthenticationFilter.setAuthenticationSuccessHandler(dryadLoginSuccessHandler);
        mobileAuthenticationFilter.setEventPublisher(defaultAuthenticationEventPublisher);
        mobileAuthenticationFilter.setAuthenticationEntryPoint(new DryadAuthExceptionEntryPoint(objectMapper));

        MobileAuthenticationProvider mobileAuthenticationProvider = new MobileAuthenticationProvider();
        mobileAuthenticationProvider.setUserDetailsService(dryadUserDetailsServiceImpl);
        http.authenticationProvider(mobileAuthenticationProvider).addFilterAfter(mobileAuthenticationFilter,
                UsernamePasswordAuthenticationFilter.class);
    }

}
